package com.skystep.drools.person.controller;

import com.skystep.drools.person.entity.Person;
import com.skystep.drools.rule.service.RuleEngineService;
import org.drools.core.base.RuleNameEqualsAgendaFilter;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("rule")
public class PersonRuleController {
    @Autowired
    private RuleEngineService ruleEngineService;

    @PostMapping("test")
    public void fireAllRules4One(@RequestBody Person person) {
        KieSession kSession = ruleEngineService.getKieContainer().newKieSession();
        try {
            kSession.insert(person);
            kSession.fireAllRules();
        } finally {
            kSession.dispose();
        }
        return;
    }

    @GetMapping("reload")
    public void reload() {
        ruleEngineService.reload();
    }

    @PostMapping("list")
    public void fireAllRules4List(@RequestBody List<Person> persons) {
        KieSession kSession = ruleEngineService.getKieContainer().newKieSession();
        try {
            for (Person person : persons) {
                kSession.insert(person);
            }
            kSession.fireAllRules();
        } finally {
            kSession.dispose();
        }
    }
}
